package controller.listener.rules;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JTable;

import components.Component;

import view.grammardevelopment.ComponentPanel;
import view.rules.SelectFeatStructView;
import features.Feature;

public class SelectFeatStructController {

	SelectFeatStructView view;
	ComponentPanel currSelectedPanel;
	AddRuleController addRuleController;
	
	public SelectFeatStructController(AddRuleController addRuleController){
		
		this.addRuleController = addRuleController;
		view = new SelectFeatStructView();
		view.getBtnOK().addActionListener(new OKActionListener());
		view.getBtnCancel().addActionListener(new CancelActionListener());
	}
	
	public void activateFrame(ComponentPanel currSelectedPanel){
		this.currSelectedPanel = currSelectedPanel;
		setFeatureTable(currSelectedPanel.getComponent().getFeatures());
		if(!view.isActive()){
			view.setVisible(true);
		}
	}
	
	public void disposeFrame(){
		if(view!=null)
			view.dispose();
	}
	
	public void setFeatureTable(ArrayList<Feature> featList){
		view.createTable(currSelectedPanel.getComponent().getName());
		
		for(int i = 1; i <= view.getFeatureTable().getColumnCount(); i+=2)
		{
			for(int j = 0; j < view.getFeatureTable().getRowCount(); j++)
			{
				for(int k = 0; k < featList.size(); k++)	
					if(view.getFeatureTable().getValueAt(j, i)!=null)
						if(view.getFeatureTable().getValueAt(j, i).equals(featList.get(k).getValue()) 
								&& view.getFeatureTable().getColumnName(i).equals(featList.get(k).getName()))
							view.getFeatureTable().setValueAt(Boolean.TRUE, j, i-1);
			}
		}
	}
	
	class OKActionListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent arg0) {

			 //insert basic feature
			 addFeatureToComponent(currSelectedPanel.getComponent(), view.getFeatureTable());
			 view.dispose();
		}
		
		public void addFeatureToComponent(Component component, JTable table)
		{
			component.getFeatures().clear();
			
			for(int i = 1 ; i <= table.getColumnCount(); i+=2)
			{
			  String featureName = table.getColumnName(i);
			  //DBFeatureValues dbFeatValList = FeatureManager.getFeatureValues(featureElement);
			  for(int j = 0; j < table.getRowCount(); j++)
			  {
				  if(table.getValueAt(j, i)!=null)
				  {		 
					  String featureValue = table.getValueAt(j, i).toString();
					  if( table.getValueAt(j, i-1) != Boolean.FALSE){
						  component.addFeature(new Feature(featureName, featureValue, true));
						  if(currSelectedPanel.getRootDocPanel().getName().equals("TO")){
								addRuleController.getStructRule().editFeature(currSelectedPanel.getComponent(), featureName,featureValue);  
								addRuleController.updateLogText();
						  }
					  }
					  else{
						  component.removeFeature(featureName, featureValue);
						  if(currSelectedPanel.getRootDocPanel().getName().equals("TO")){
							  addRuleController.getStructRule().removeFeature(currSelectedPanel.getComponent(), featureName, featureValue);
							  addRuleController.updateLogText();
						  }
					  }
						  
					  
				  }  
			  }
		  	}
		}
	}
	
	class CancelActionListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			view.dispose();
		}
		
	}
	
	
	
}
